package com.iiot.redis;

import com.alibaba.fastjson.JSON;
import java.util.HashSet;
import java.util.Set;

import com.iiot.util.ExceptionUtil;
import com.iiot.util.PropertiesUtilLocal;
import org.apache.log4j.Logger;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

public class JedisBSJFactory {
    static JedisCluster jc;
    static Logger logger = Logger.getLogger(JedisBSJFactory.class);
    protected static final int DEFAULT_TIMEOUT = 2000;
    protected static final int DEFAULT_MAX_REDIRECTIONS = 5;

    private static void CreateJedis() {
        try {
            JedisPoolConfig config = new JedisPoolConfig();
            int maxIdle = Integer.parseInt(PropertiesUtilLocal.getConfig("redis.max-idle"));
            config.setMaxIdle(maxIdle);
            int maxTotal = Integer.parseInt(PropertiesUtilLocal.getConfig("redis.max-total"));
            config.setMaxTotal(maxTotal);
            int maxWaitMillis = Integer.parseInt(PropertiesUtilLocal.getConfig("redis.max-wait-millis"));
            config.setMaxWaitMillis(maxWaitMillis);
            // 多节点并行执行数目
            int redisParallelNum = Integer.parseInt(PropertiesUtilLocal.getConfig("redis.max-attempts"));
            // config.setTestOnBorrow(true);
            Set<HostAndPort> nodes = new HashSet<HostAndPort>();
            String hostports = PropertiesUtilLocal.getConfig("redis.nodes");
            if (hostports != null) {
                String[] singleHostPort = hostports.split(";");
                for (String hostandport : singleHostPort) {
                    if (hostandport != null && !hostandport.equals("")) {
                        String[] split = hostandport.split(":");
                        String host = split[0];
                        String portStr = split[1];
                        int port = Integer.parseInt(portStr.trim());
                        nodes.add(new HostAndPort(host, port));
                    }
                }
            } else {
                String hosts = PropertiesUtilLocal.getConfig("redis.host");
                String[] hostArr = hosts.split(";");
                int port = Integer.parseInt(PropertiesUtilLocal.getConfig("redis.port"));
                for (String host : hostArr) {
                    nodes.add(new HostAndPort(host, port));
                }
            }

            String redisPsw = PropertiesUtilLocal.getConfig("redis.password");
            logger.info(
                    "redis配置：nodes: "
                            + JSON.toJSONString(nodes)
                            + ", maxIdle: "
                            + maxIdle
                            + ", maxTotal: "
                            + maxTotal
                            + ", maxWaitMillis: "
                            + maxWaitMillis
                            + "/redisPsw:"
                            + redisPsw);
            // 没有密码就不使用密码
            if (redisPsw == null || redisPsw.isEmpty()) {
                logger.info("REDIS不使用密码");
                jc = new JedisCluster(nodes, redisParallelNum, config);
            } else {
                logger.info("REDIS使用密码:");
                jc = new JedisCluster(
                        nodes,
                        DEFAULT_TIMEOUT,
                        DEFAULT_TIMEOUT,
                        DEFAULT_MAX_REDIRECTIONS,
                        redisPsw,
                        config);
            }

        } catch (Exception e) {
            logger.error("添加REDIS组失败: " + ExceptionUtil.getStackStr(e));
        }
    }

    public static JedisCluster getJedis() {
        if (null == jc) {
            CreateJedis();
        }
        return jc;
    }
}
